#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Controls;

namespace UTable.Objects
{
    public class AnimationUtility
    {
		public static TimeSpan AnimationTime = TimeSpan.FromMilliseconds(500);

        public static AnimationTimeline CreateThicknessAnimation(Thickness to, TimeSpan duration)
        {
            SplineThicknessKeyFrame endKeyframe = new SplineThicknessKeyFrame();
            endKeyframe.Value = to;
            endKeyframe.KeyTime = KeyTime.FromTimeSpan(duration);

            KeySpline ks = new KeySpline();
            ks.ControlPoint1 = new Point(0, 1);
            ks.ControlPoint2 = new Point(1, 1);
            endKeyframe.KeySpline = ks;

            // Create Animation
            ThicknessAnimationUsingKeyFrames animation = new ThicknessAnimationUsingKeyFrames();
            animation.KeyFrames.Add(endKeyframe);
            return animation;
        }

        public static AnimationTimeline CreatePointAnimation(Point to, TimeSpan duration)
        {
            SplinePointKeyFrame endKeyframe = new SplinePointKeyFrame();
            endKeyframe.Value = to;
            endKeyframe.KeyTime = KeyTime.FromTimeSpan(duration);

            KeySpline ks = new KeySpline();
            ks.ControlPoint1 = new Point(0, 1);
            ks.ControlPoint2 = new Point(1, 1);
            endKeyframe.KeySpline = ks;

            // Create Animation
            PointAnimationUsingKeyFrames animation = new PointAnimationUsingKeyFrames();
            animation.KeyFrames.Add(endKeyframe);
            return animation;
        }

        public static void DoPointAnimation(Point to, TimeSpan duration, DependencyProperty property, UIElement target, bool holdend)
        {
            AnimationTimeline animation = AnimationUtility.CreatePointAnimation(to, duration);
            if (holdend)
                animation.FillBehavior = FillBehavior.HoldEnd;
            else
                animation.FillBehavior = FillBehavior.Stop;
            animation.Completed += new EventHandler(delegate(Object obj, EventArgs args)
            {
                if (!holdend)
                    target.SetValue(property, to);
            });
            target.BeginAnimation(property, animation);
        }

        public static AnimationTimeline CreateDoubleAnimation(double to, TimeSpan duration)
        {
            SplineDoubleKeyFrame endKeyframe = new SplineDoubleKeyFrame();
            endKeyframe.Value = to;
            endKeyframe.KeyTime = KeyTime.FromTimeSpan(duration);

            KeySpline ks = new KeySpline();
            ks.ControlPoint1 = new Point(0, 1);
            ks.ControlPoint2 = new Point(1, 1);
            endKeyframe.KeySpline = ks;

            // Create Animation
            DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();
            animation.KeyFrames.Add(endKeyframe);
            return animation;
        }

		public static void DoDoubleAnimation(double to, TimeSpan duration, DependencyProperty property, UIElement target)
		{
			DoDoubleAnimation(to, duration, property, target, false);
		}

		public static void DoDoubleAnimation(double to, TimeSpan duration, DependencyProperty property, UIElement target, bool holdend)
		{
			AnimationTimeline animation = AnimationUtility.CreateDoubleAnimation(to, duration);
			if (holdend)
				animation.FillBehavior = FillBehavior.HoldEnd;
			else
				animation.FillBehavior = FillBehavior.Stop;
			animation.Completed += new EventHandler(delegate(Object obj, EventArgs args)
			{
				if (!holdend)
					target.SetValue(property, to);
			});
			target.BeginAnimation(property, animation);
		}

		public static void DoDoubleAnimation(double to, TimeSpan duration, DependencyProperty property, UIElement target, EventHandler completedHandler)
		{
			AnimationTimeline animation = AnimationUtility.CreateDoubleAnimation(to, duration);
			animation.FillBehavior = FillBehavior.Stop;
			animation.Completed += completedHandler;
			target.BeginAnimation(property, animation);
		}
    }
}
